package net.elive.peer.seek;

import java.net.InetAddress;
import java.net.UnknownHostException;
import java.util.BitSet;

import net.elive.peer.p2p.P2PAP;
import net.elive.peer.share.CacheFileInfo;
import net.elive.tools.ByteIntSwitch;
import net.elive.tools.ByteLongSwitch;

/**
 * 资源节点
 * 
 * @author jincool.cao@gmail.com
 * 
 */
public class SourceNode {
	/**
	 * 资源SessionID
	 */
	private int sourceSessionId;
	/**
	 * 协服务器节点IP
	 */
	private InetAddress assistNodeIp;
	/**
	 * 协服务器端口
	 */
	private int assistNodePort;
	/**
	 * 协服务器用户SessionId
	 */
	private int assistSessionId;
	/**
	 * 文件块集合
	 */
	public BitSet blockBit = new BitSet();
	/**
	 * 文件块总数
	 */
	private int blockSum;
	/**
	 * 缓存信息
	 */
	private CacheFileInfo cacheFile;
	/**
	 * 更新时间
	 */
	private long updateTime = P2PAP.getVTime();
	/**
	 * 内网IP
	 */
	public InetAddress lanIP;
	/**
	 * 外网IP
	 */
	public InetAddress outIP;
	/**
	 * 
	 * @param sourceSessionId	资源SessionID
	 * @param assistNodeIp		协服务器IP
	 * @param assistNodePort	协服务器端口
	 * @param assistSessionId 协服务器SessionID
	 * @param lanIP	内网IP
	 * @param outIP 外网IP
	 */
	public SourceNode(int sourceSessionId, InetAddress assistNodeIp,
			int assistNodePort, int assistSessionId, InetAddress lanIP,
			InetAddress outIP) {
		this.sourceSessionId = sourceSessionId;
		this.assistNodeIp = assistNodeIp;
		this.assistNodePort = assistNodePort;
		this.assistSessionId =assistSessionId;
		this.lanIP = lanIP;
		this.outIP = outIP;
		cacheFile  = new CacheFileInfo();
		blockBit  = new BitSet(blockSum);
	}
	public SourceNode(byte[] data) {
		updateTime = ByteLongSwitch.toLong(data, 0);
		sourceSessionId  =ByteIntSwitch.toInt(data, 8);
		byte[] b = new byte[4];
		System.arraycopy(data, 12, b, 0, 4);
		try {
			assistNodeIp = InetAddress.getByAddress(b);
		} catch (UnknownHostException e) {
			e.printStackTrace();
		}
		assistNodePort =ByteIntSwitch.toInt(data, 16);
		assistSessionId =ByteIntSwitch.toInt(data, 20);
		
		byte[] b2 = new byte[4];
		System.arraycopy(data, 24, b2, 0, 4);
		outIp = InetAddress.getByAddress(b2);
		
	}
}
